package com.winshare.saleAssistant.service.impl;

import com.winshare.saleAssistant.dao.ProductInfoVersionMapper;
import com.winshare.saleAssistant.domain.dto.req.BaseIdDTO;
import com.winshare.saleAssistant.domain.dto.req.ProductInfoVersionCreateDTO;
import com.winshare.saleAssistant.domain.dto.req.ProductInfoVersionQueryDTO;
import com.winshare.saleAssistant.domain.dto.req.ProductInfoVersionUpdateDTO;
import com.winshare.saleAssistant.domain.dto.resp.ProductInfoVersionRespDTO;
import com.winshare.saleAssistant.domain.entity.ProductInfoVersionDO;
import com.winshare.saleAssistant.domain.query.ProductInfoVersionQuery;
import com.winshare.saleAssistant.exception.BusinessException;
import com.winshare.saleAssistant.mapper.ProductInfoVersionMapStruct;
import com.winshare.saleAssistant.service.ProductInfoVersionService;
import com.winshare.saleAssistant.util.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductInfoVersionServiceImpl implements ProductInfoVersionService {

    private final ProductInfoVersionMapper productInfoVersionMapper;

    public ProductInfoVersionServiceImpl(ProductInfoVersionMapper productInfoVersionMapper) {
        this.productInfoVersionMapper = productInfoVersionMapper;
    }

    @Override
    public void saveProductInfoVersion(ProductInfoVersionCreateDTO productInfoVersionCreateDTO) {
        ProductInfoVersionDO productInfoVersionDO = ProductInfoVersionMapStruct
                .INSTANCE.fromProductInfoVersionCreateDTO(productInfoVersionCreateDTO);
        if(checkVersionExist(productInfoVersionDO)) {
            throw new BusinessException("同一产品下不能含有相同的版本号或版本名称");
        }
        productInfoVersionMapper.saveProductInfoVersion(productInfoVersionDO);

    }

    @Override
    public void updateProductInfoVersion(ProductInfoVersionUpdateDTO productInfoVersionUpdateDTO) {
        ProductInfoVersionDO productInfoVersionDO = ProductInfoVersionMapStruct
                .INSTANCE.fromProductInfoVersionUpdateDTO(productInfoVersionUpdateDTO);
        if(checkVersionExist(productInfoVersionDO)) {
            throw new BusinessException("同一产品下不能含有相同的版本号或版本名称");
        }
        productInfoVersionMapper.updateProductInfoVersion(productInfoVersionUpdateDTO);
    }

    @Override
    public void deleteProductInfoVersion(BaseIdDTO baseIdDTO) {
        ProductInfoVersionDO productInfoVersionDO = ProductInfoVersionMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        ProductInfoVersionDO productInfoVersionDOInSql = productInfoVersionMapper
                .getProductInfoVersion(productInfoVersionDO);
        if(null == productInfoVersionDOInSql) {
            throw new BusinessException("当前版本不存在");
        }
        productInfoVersionMapper.deleteProductInfoVersion(productInfoVersionDO);
    }

    @Override
    public ProductInfoVersionRespDTO getProductInfoVersionDetail(BaseIdDTO baseIdDTO) {
        ProductInfoVersionDO productInfoVersionDO = ProductInfoVersionMapStruct.INSTANCE.fromBaseIdDTO(baseIdDTO);
        ProductInfoVersionDO productInfoVersionDOInSql = productInfoVersionMapper
                .getProductInfoVersion(productInfoVersionDO);
        if(null == productInfoVersionDOInSql) {
            throw new BusinessException("当前版本不存在");
        }
        ProductInfoVersionRespDTO productInfoVersionRespDTO = ProductInfoVersionMapStruct.INSTANCE
                .fromProductInfoVersionDO(productInfoVersionDOInSql);
        return productInfoVersionRespDTO;
    }

    @Override
    public Page<ProductInfoVersionRespDTO> getProductInfoVersionPage(ProductInfoVersionQueryDTO
                                                                                 productInfoVersionQueryDTO) {
        ProductInfoVersionQuery productInfoVersionQuery = ProductInfoVersionMapStruct.INSTANCE
                .fromProductInfoVersionQueryDTO(productInfoVersionQueryDTO);
        productInfoVersionQuery.setOffsetIndex(productInfoVersionQueryDTO.getOffsetIndex());
        int totalCount = productInfoVersionMapper.queryProductInfoVersionCount(productInfoVersionQuery);
        Integer pageIndex = productInfoVersionQueryDTO.getPageIndex();
        Integer pageSize = productInfoVersionQueryDTO.getPageSize();
        if(0 == totalCount) {
            return new Page<>(pageIndex,pageSize,totalCount,new ArrayList<>());
        }
        List<ProductInfoVersionDO>  productInfoVersionDOList = productInfoVersionMapper
                .findProductInfoVersionListByConditions(productInfoVersionQuery);
        List<ProductInfoVersionRespDTO> productInfoVersionRespDTOList = ProductInfoVersionMapStruct.INSTANCE
                .fromProductInfoVersionDOList(productInfoVersionDOList);
        return new Page<>(pageIndex,pageSize,totalCount,productInfoVersionRespDTOList);
    }

    private boolean checkVersionExist(ProductInfoVersionDO productInfoVersionDO) {
        return productInfoVersionMapper.countProductInfoVersionByProductInfoDO(productInfoVersionDO) > 0 ;
    }
}
